package com.springboot.huli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.huli.entity.RoleMenu;
import com.springboot.huli.entity.Roles;
import com.springboot.huli.mapper.RoleMenuMapper;
import com.springboot.huli.mapper.RolesMapper;
import com.springboot.huli.qpage.QPage;
import com.springboot.huli.service.IRolesService;
import com.springboot.huli.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-05-10
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements IRolesService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 分页查询角色
     * @param qPage
     * @return
     */
    @Override
    public IPage getPageList(QPage qPage, Roles roles) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        List<Roles> mapList = baseMapper.list(iPage,roles);
        return iPage.setRecords(mapList);
    }


    /**
     * 根据角色ID获取已经分配的菜单ID
     * @param roleid
     * @return
     */
    @Transactional
    @Override
    public List<Integer> getMenuByRoleId(Integer roleid) {
        return this.baseMapper.getMenuByRoleId(roleid);
    }

    /**
     * 根据角色ID修改当前拥有的菜单结构
     * @param menuids
     * @param roleid
     * @return
     */
    @Transactional
    @Override
    public Result updateMenuByRoleId(String menuids, Integer roleid) {
        //第一步先删除中间表
        QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<RoleMenu>();
        queryWrapper.eq("roleid",roleid);
        this.roleMenuMapper.delete(queryWrapper);
        //第二步根据选择菜单进行角色菜单中间表插入
        if(!StringUtils.isEmpty(menuids)){
            String[] mlist=menuids.split(",");
            if(mlist!=null&&mlist.length>=1){
                for (String menuid : mlist) {
                    if(menuid.equals("0")){
                        continue;//排除0为全选的值，是不需要的
                    }
                    RoleMenu roleMenu=new RoleMenu();
                    roleMenu.setRoleid(roleid);
                    roleMenu.setMenuid(Integer.parseInt(menuid));
                    this.roleMenuMapper.insert(roleMenu);
                }
            }
        }
        return Result.reOk("分配菜单结构成功");
    }


    /**
     * 新增角色保存
     * @param role
     * @return
     */
    @Transactional
    @Override
    public Result saveRole(Roles role) {
        QueryWrapper<Roles> queryWrapper=new QueryWrapper<Roles>();
        queryWrapper.eq("rolename",role.getRolename());
        if(this.getOne(queryWrapper)!=null){
            return Result.reError("此角色名称已经存在，请重新取名");
        }
        if(this.save(role)){
            return Result.reOk("恭喜，新增角色成功");
        }
        return Result.reError("新增角色出错");
    }

    /**
     * 修改角色保存
     * @param role
     * @return
     */
    @Transactional
    @Override
    public Result updateRole(Roles role) {
        QueryWrapper<Roles> queryWrapper=new QueryWrapper<Roles>();
        queryWrapper.eq("rolename",role.getRolename());
        Roles newrole=this.getOne(queryWrapper);
        if(newrole!=null&&newrole.getRoleid()!=role.getRoleid()){
            return Result.reError("此角色名称已经存在，请重新取名");
        }
        if(this.updateById(role)){
            return Result.reOk("恭喜，修改角色成功");
        }
        return Result.reError("修改角色出错");
    }

    /**
     * 根据角色ID获取角色信息
     * @param roleid
     * @return
     */
    @Override
    public Roles getRoleById(Integer roleid) {
        return this.getById(roleid);
    }

}
